﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.ServiceFacade;
using Kingdee.BOS.Util;
using System.ComponentModel;
using System.Text;

namespace HX.HxDemo.BOS.Business.PlugIn25011703
{
    [Description("【表单插件】DataChanged"),HotUpdate]
    public class DataChangedFormPlugin : AbstractDynamicFormPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            var msg = string.Format("变更对象：{0}.{1}.{2}[{3}.{4}.{5}]\r\n行号：{6}\r\n旧值：{7}\r\n新值：{8}\r\n完整数据包：\r\n{9}",
                this.View.BillBusinessInfo.GetForm().Name, e.Field.Entity.Name, e.Field.Name
                , this.View.BillBusinessInfo.GetForm().Id, e.Field.Entity.Key, e.Field.Key
                , e.Row, e.OldValue, e.NewValue, DynamicObjectToJson(this.Model.GetValue(e.Field, e.Row)));
            this.View.ShowMessage(msg);
        }

        private static object DynamicObjectToJson(object obj)
        {
           if(obj == null)
            {
                return null;
            }
            var jsonSerializerProxy = new JsonSerializerProxy(Encoding.UTF8, false);
            var jsonData = jsonSerializerProxy.Serialize(obj);
            return jsonData;
        }
    }
}
